package com.zpb.framework.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zpb.framework.domain.ApiResult;
import com.zpb.framework.domain.dto.TagDto;
import com.zpb.framework.domain.entity.Tag;
import com.zpb.framework.domain.enums.ResultCodeEnum;
import com.zpb.framework.domain.vo.PageVo;
import com.zpb.framework.domain.vo.TagSimpleVo;
import com.zpb.framework.domain.vo.TagVo;
import com.zpb.framework.mapper.TagMapper;
import com.zpb.framework.service.TagService;
import com.zpb.framework.utils.BeanCopyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description:
 * @Author: zhaopengbo
 * @Date: 2025/9/17
 */
@Service
public class TagServiceImpl extends ServiceImpl<TagMapper,Tag> implements TagService  {
    @Autowired
    private TagMapper tagMapper;
    @Override
    public ApiResult<PageVo> list(Integer pageNum, Integer pageSize, TagDto tagDto) {
        LambdaQueryWrapper<Tag> wrapper = new LambdaQueryWrapper<Tag>();
        wrapper.like(tagDto.getName() !=null,Tag::getName, tagDto.getName())
                .like(tagDto.getRemark()!=null,Tag::getRemark,tagDto.getRemark())
                .orderByDesc(Tag::getUpdateTime);
        Page<Tag> tagPage = new Page<>(pageNum, pageSize);
        page(tagPage, wrapper);
        List<Tag> tags = tagPage.getRecords();
        List<TagVo> tagVos = BeanCopyUtil.copyBeanList(tags, TagVo.class);
        PageVo pageVo = new PageVo(tagPage.getTotal(), tagVos);

        return ApiResult.success(pageVo);
    }

    @Override
    public ApiResult addTag(TagDto tagDto) {
        int result = tagMapper.insert(BeanCopyUtil.copyBean(tagDto, Tag.class));
        if(result>0){
            return ApiResult.success();
        }
        return ApiResult.failure(ResultCodeEnum.ADD_FAILURE);
    }
    @Override
    public ApiResult delTag(Long id){
        int result = tagMapper.deleteById(id);
        if(result>0){
            return ApiResult.success();
        }
        return ApiResult.failure(ResultCodeEnum.DEL_FAILURE);
    }

    @Override
    public ApiResult<TagVo> getTag(Long id) {
        Tag tag = tagMapper.selectById(id);
        if(tag==null){
            return ApiResult.failure(ResultCodeEnum.NO_TAG);
            }
        TagVo tagVo = BeanCopyUtil.copyBean(tag, TagVo.class);
        return ApiResult.success(tagVo);
    }
    @Override
    public ApiResult update(TagDto tagDto){
        int result = tagMapper.updateById(BeanCopyUtil.copyBean(tagDto, Tag.class));
        if (result>0){
            return ApiResult.success();
        }
        return ApiResult.failure(ResultCodeEnum.UPDATE_FAILURE);
    }

    @Override
    public ApiResult<List<TagSimpleVo>> getAll() {
        List<Tag> tags = tagMapper.selectList(null);
        return ApiResult.success(BeanCopyUtil.copyBeanList(tags, TagSimpleVo.class));
    }
}
